package com.liucxu.jiandanl.View.Activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.liucxu.jiandanl.R;
import com.liucxu.jiandanl.View.Fragment.BoringPicsFragment;
import com.liucxu.jiandanl.View.Fragment.FreshNewsFragment;
import com.liucxu.jiandanl.View.Fragment.JokeFragment;
import com.liucxu.jiandanl.View.Fragment.GirlsFragment;

public class MainActivity extends FragmentActivity {

    private FragmentTabHost fragmentTabHost;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    /**
     * 初始化TabHost
     */
    private void initView(){
        fragmentTabHost=(FragmentTabHost)findViewById(android.R.id.tabhost);
        fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.main_content);
        fragmentTabHost.getTabWidget().setDividerDrawable(null);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("FreshNews").setIndicator(getView("新鲜事",R.drawable.tab_fresh_news_res)), FreshNewsFragment.class,null);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("BoringPics").setIndicator(getView("无聊图",R.drawable.tab_boring_pics_res)), BoringPicsFragment.class,null);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Jokes").setIndicator(getView("段子",R.drawable.tab_jokes_res)), JokeFragment.class,null);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Setting").setIndicator(getView("妹纸",R.drawable.tab_setting_res)), GirlsFragment.class,null);
    }

    /**
     * Tab 设置文字以及图标
     * @param label
     * @param imgRsc
     */
    private View getView(String label,int imgRsc){
        LayoutInflater inflater=LayoutInflater.from(this);
        View view=inflater.inflate(R.layout.item_tab,null);
        RelativeLayout tab_root=(RelativeLayout)view.findViewById(R.id.tab_root);
        TextView labelTv=(TextView)view.findViewById(R.id.item_label);
        ImageView imgTab=(ImageView)view.findViewById(R.id.item_img);
        labelTv.setText(label);
        imgTab.setImageResource(imgRsc);
        tab_root.setBackgroundResource(R.drawable.tab_root_bg);
        return view;
    }
}
